package com.xbongbong.saas.enums.business;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.ScreenTypeEnum;
import com.xbongbong.pro.constant.CommonConstant;
import com.xbongbong.pro.constant.I18nStringConstant;
import com.xbongbong.pro.enums.PaasStageEnum;
import com.xbongbong.saas.enums.SetTypeEnum;
import com.xbongbong.saas.enums.ShowTypeEnum;
import com.xbongbong.saas.enums.base.ManagementBaseEnum;
import lombok.Getter;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Getter
public enum ClueEnum {
    // 基本信息
    BASE_INFO("other_1", "enum.clue.attrName.base_info", "baseInfo", FieldTypeEnum.SEPARATOR.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 10000, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    //评分
    SCORE("num_1", "enum.clue.attrName.score", "score", FieldTypeEnum.NUM.getType(), 1, 0, 1, ShowTypeEnum.LIST_DETAIL.getCode(), 9995, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    //企业名称
    COMPANY_NAME("text_1", "enum.clue.attrName.company_name", "name", FieldTypeEnum.TEXT.getType(), SetTypeEnum.DISABLE_OPEN.getCode(), 0, 0, ShowTypeEnum.ALL.getCode(), 9990, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    //联系人
    CONTACT("text_2", "enum.clue.attrName.contact", "contact", FieldTypeEnum.TEXT.getType(), SetTypeEnum.DISABLE_NONE.getCode(), 1, 0, ShowTypeEnum.ALL.getCode(), 9980, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    //电话
    PHONE("subForm_1", "enum.clue.attrName.phone", "phone", FieldTypeEnum.CONTACT_NUMBER.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9970, 1, 1, ScreenTypeEnum.SCREEN_SEARCH.getType()),
    //地址
    ADDRESS("address_1", "enum.clue.attrName.address", "address", FieldTypeEnum.ADDRESS.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9960, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    //关联市场活动
    MARKET_ACTIVITY_ID("text_3", "enum.clue.attrName.market_activity_id", "marketActivityId", FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9950, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    //标签
    LABEL("array_1", "enum.clue.attrName.label", "label", FieldTypeEnum.LABEL.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 0, 0, ShowTypeEnum.ALL.getCode(), 9940, 1, 0, ScreenTypeEnum.SCREEN_SEARCH.getType()),
    //线索来源
    CLUE_SOURCE("text_4", "enum.clue.attrName.clue_source", "clueSource", FieldTypeEnum.COMBO.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9930, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    //线索状态
    CLUE_STATUS("text_5", "enum.clue.attrName.clue_status", "clueStatus", FieldTypeEnum.COMBO.getType(), 3, 0, 0, ShowTypeEnum.ALL.getCode(), 9920, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    CLUE_STAGE("text_6", "enum.clue.attrName.clue_stage", "clueStage", FieldTypeEnum.STAGE_THRUSTER.getType(), 3, 1, 0, ShowTypeEnum.ALL.getCode(), 9910, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    INVALID_REASON("text_7", "enum.clue.attrName.invalid_reason", "invalidReason", FieldTypeEnum.COMBO.getType(), SetTypeEnum.DISABLE_NONE.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 9900, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    //无效备注
    INVALID_MEMO("text_8", "enum.clue.attrName.invalid_memo", "invalidMemo", FieldTypeEnum.TEXTAREA.getType(), SetTypeEnum.DISABLE_NONE.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 9890, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    //负责人
    OWNER_ID("ownerId", "enum.clue.attrName.owner_id", "ownerId", FieldTypeEnum.OWNERID.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9880, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    //协同人
    COUSERID("coUserId", "enum.clue.attrName.couserid", "coUserId", FieldTypeEnum.COUSERID.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9870, 1, 0, ScreenTypeEnum.SCREEN_SEARCH.getType()),
    // 扩展信息
    EXTEND_INFO("other_2", "enum.clue.attrName.extend_info", "extendInfo", FieldTypeEnum.SEPARATOR.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 9860, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    //图片
    IMAGE("file_1", "enum.clue.attrName.image", "image", FieldTypeEnum.IMAGE.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.ALL.getCode(), 9850, 1, 0, ScreenTypeEnum.SCREEN_SHOW.getType()),
    //文件
    FILE("file_2", "enum.clue.attrName.file", "file", FieldTypeEnum.UPLOAD.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.ALL.getCode(), 9840, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    //备注
    MEMO("text_9", "enum.clue.attrName.memo", "memo", FieldTypeEnum.TEXTAREA.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.ALL.getCode(), 9830, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    // 系统信息
    SYSTEM_INFO("other_3", "enum.clue.attrName.system_info", "systemInfo", FieldTypeEnum.SEPARATOR.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 9820, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    //创建人
    CREATOR_ID("creatorId", "enum.clue.attrName.creator_id", "creatorId", FieldTypeEnum.CREATORID.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9810, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    //创建时间
    ADD_TIME("addTime", "enum.clue.attrName.add_time", "addTime", FieldTypeEnum.ADDTIME.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9800, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    //更新时间
    UPDATE_TIME("updateTime", "enum.clue.attrName.update_time", "updateTime", FieldTypeEnum.UPDATETIME.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9790, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    // 不在模板设置界面展示字段，但需要在列表详情展示
    LAST_CONNECT_TIME("date_1", "enum.clue.attrName.last_connect_time", "lastConnectTime", FieldTypeEnum.DATETIME.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9770, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    //公海退回时间
    BACK_PUBLIC_DATE("date_2", "enum.clue.attrName.back_public_date", "backPublicDate", FieldTypeEnum.DATETIME.getType(), 1, 1, 1, ShowTypeEnum.LIST.getCode(), 9760, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    //分配时间
    DISTRIBUTION_DATE("date_3", "enum.clue.attrName.distribution_date", "distributionDate", FieldTypeEnum.DATETIME.getType(), 1, 1, 1, ShowTypeEnum.LIST.getCode(), 9750, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    //是否公海池
    IS_PUBLIC("num_2", "enum.clue.attrName.is_public", "isPublic", FieldTypeEnum.COMBO_NUM.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9740, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    //是否归档
    IS_ARCHIVED("num_3", "enum.clue.attrName.is_archived", "isArchived", FieldTypeEnum.COMBO_NUM.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9730, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    //关联市场活动名称
    MARKET_ACTIVITY_NAME("text_10", "enum.clue.attrName.market_activity_name", "marketActivityName", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9720, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),

    //阶段流程id
    STAGE_PROCESS_ID(PaasStageEnum.STAGE_PROCESS_ID.getAttr(), "enum.clue.attrName.stage_process_id", PaasStageEnum.STAGE_PROCESS_ID.getSaasAttr(), FieldTypeEnum.LONG.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9980, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    //阶段id
    STAGE_ID(PaasStageEnum.STAGE_ID.getAttr(), "enum.clue.attrName.attrName.stage_id", PaasStageEnum.STAGE_ID.getSaasAttr(), FieldTypeEnum.LONG.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9981, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    //阶段比率
    STAGE_RATIO(PaasStageEnum.STAGE_RATIO.getAttr(),"enum.clue.attrName.attrName.stage_ratio", PaasStageEnum.STAGE_RATIO.getSaasAttr(), FieldTypeEnum.STAGE_RATIO.getType(), ScreenTypeEnum.SCREEN_GROUP.getType(), 0, 0, ShowTypeEnum.ALL.getCode(), 9911, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),

    //退回次数
    BACK_NUM("num_61", "enum.clue.attrName.back_num", "backNum", FieldTypeEnum.NUM.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9761, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    //公海分组
    PUBLIC_GROUP("text_81", "enum.clue.attrName.public_group", "publicGroup", FieldTypeEnum.PUBLIC_GROUP.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9760, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),

    ;

    /**
     * 英文名称
     */
    private String attr;
    /**
     * 中文名称
     */
    private String attrName;
    /**
     * 对应saas中的attr
     */
    private String saasAttr;
    /**
     * 字段类型，详见
     *
     * @see FieldTypeEnum
     */
    private Integer fieldType;
    /**
     * 重命名、判重、启用必填等权限
     * 1.重命名+字段权限
     * 2.重命名+字段权限+查重
     * 3.重命名+字段权限+启用、必填
     * 4.重命名+字段权限+查重+启用、必填
     * 注：pro中启用和必填是同一个权限，同时启用或同时关闭
     */
    private Integer setType;
    /**
     * 模板设计阶段字段是否允许编辑
     * 1：允许编辑
     * 0：不允许编辑
     */
    private Integer designEditable;
    /**
     * 字段是否显示在模板设计界面
     * 1：不显示
     * 0：显示
     */
    private Integer editHide;
    /**
     * 字段在各个界面（新建、编辑、列表、详情）的展现形式
     * 详见
     *
     * @see ShowTypeEnum
     */
    private Integer showType;
    /**
     * 字段在模板设计界面的排序
     * 建议不要用连续的数字，可以用等差数列，以防以后在两个字段之间加入新字段
     */
    private Integer sort;
    /**
     * 字段在各个界面是否可见(启用)
     * 1：可见
     * 0：不可见
     * 注：字段在具体页面的可见性需要用属性和showType联合判断，在visible=1的情况下判断showType
     */
    private Integer visible;
    /**
     * 字段是否必填
     * 1：必填
     * 0：不必填
     */
    private Integer required;
    /**
     * 筛选类型
     * 1：可筛选
     * 2：不可筛选
     */
    private Integer screenType;

    ClueEnum(String attr, String attrName, String saasAttr, Integer fieldType, Integer setType, Integer designEditable,
                       Integer editHide, Integer showType, Integer sort, Integer visible, Integer required, Integer screenType) {
        this.attr = attr;
        this.attrName = attrName;
        this.saasAttr = saasAttr;
        this.fieldType = fieldType;
        this.setType = setType;
        this.designEditable = designEditable;
        this.editHide = editHide;
        this.showType = showType;
        this.sort = sort;
        this.visible = visible;
        this.required = required;
        this.screenType = screenType;
    }

    /**
     * 对data内的字段拼上“data.”
     *
     * @param cache
     * @return java.lang.String
     * @author qcy
     * @date 2020/11/2 18:12
     * @version v1.0
     * @since v1.0
     */
    public static String getAttrConnectData(ClueEnum cache) {
        return "data." + cache.getAttr();
    }

    /**
     * 获取es查询、聚合字段
     * data在库内是json对象，对data字段内某个key做es【精确查询】时，需要在其前拼上“data.”，且【text等类型的】要在后面拼“.keyword”
     *
     * @param cache 当前枚举对象
     * @return java.lang.String 处理后的聚合字段
     */
    public static String getEsAttr4Keyword(ClueEnum cache) {
        FieldTypeEnum fieldTypeEnum = FieldTypeEnum.getFieldTypeEnum(cache.fieldType);
        //字段类型为text，或者字段在es内的类型时text，则需要在后面拼上“.keyword”
        boolean flag = fieldTypeEnum == FieldTypeEnum.TEXT || "text".equals(fieldTypeEnum.getEsalias());
        if (flag) {
            return "data." + cache.getAttr() + ".keyword";
        }
        return "data." + cache.getAttr();
    }

    public static List<JSONObject> getCheckRepeatHead() {
        JSONObject customerName = new JSONObject();
        customerName.put("prop", "name");
        customerName.put("fieldType", COMPANY_NAME.fieldType);
        customerName.put("label", COMPANY_NAME.getAttrName());
        customerName.put("attr", COMPANY_NAME.attr);
        JSONObject mainUser = new JSONObject();
        mainUser.put("prop", "mainUser");
        mainUser.put("fieldType", OWNER_ID.fieldType);
        mainUser.put("label", OWNER_ID.getAttrName());
        mainUser.put("attr", OWNER_ID.attr);
        JSONObject addTime = new JSONObject();
        addTime.put("prop", "addTime");
        addTime.put("fieldType", ADD_TIME.fieldType);
        addTime.put("label", ADD_TIME.getAttrName());
        addTime.put("attr", ADD_TIME.attr);
        JSONObject clueStatus = new JSONObject();
        clueStatus.put("prop", "clueStatus");
        clueStatus.put("fieldType", CLUE_STATUS.fieldType);
        clueStatus.put("label", CLUE_STATUS.getAttrName());
        clueStatus.put("attr", CLUE_STATUS.attr);
        JSONObject position = new JSONObject();
        position.put("prop", "position");
        position.put("fieldType", FieldTypeEnum.TEXT.getType());
        position.put("label", I18nMessageUtil.getMessage(I18nStringConstant.LOCATION));
//        JSONObject formName = new JSONObject();
//        formName.put("prop", "formName");
//        formName.put("fieldType", FieldTypeEnum.TEXT.getType());
//        formName.put("label", I18nMessageUtil.getMessage(I18nStringConstant.FORM_NAME));
        JSONObject option = new JSONObject();
        option.put("prop", "option");
        option.put("fieldType", FieldTypeEnum.OPTION.getType());
        option.put("label", I18nMessageUtil.getMessage(CommonConstant.OPERATE));
        return Arrays.asList(customerName, mainUser, addTime, clueStatus, position, option);
    }


    /**
     * 拼接地址字段
     * 库内地址字段的格式为：data对象—address对象—{"province": "", "district":"", "city":"", "address": ""}
     *
     * @param addressEnum
     * @param key         类型，此处传"province"、"district"、"city"
     * @return java.lang.String 处理后的es地址聚合字段
     */
    public static String getAddress4Aggs(ClueEnum addressEnum, String key) {
        String province = "province";
        String district = "district";
        String city = "city";
        String attr = "data." + addressEnum.getAttr() + ".";
        if (province.equals(key)) {
            return attr + province + ".keyword";
        } else if (district.equals(key)) {
            return attr + district + ".keyword";
        } else if (city.equals(key)) {
            return attr + city + ".keyword";
        }
        return attr + "keyword";
    }


    /**
     * app端详情默认标签显示字段
     *
     * @return List
     */
    public static List<Object> getLabelAttrList() {
        return Arrays.asList(LABEL, CLUE_STATUS);
    }

    /**
     * 详情默认摘要
     */
    public static List<Object> getSummaryAttrList() {
        return Arrays.asList(OWNER_ID, LAST_CONNECT_TIME);
    }

    /**
     * app端详情默认标题显示字段
     */
    public static List<Object> getTitleAttrList() {
        return Collections.singletonList(COMPANY_NAME);
    }


    public String getAttrName() {
        return I18nMessageUtil.getMessage(attrName);
    }

    /**
     * 市场活动详情页tab 关联销售线索列表，需要显示的字段
     * @return
     */
    public static List<String> getMarketActivityTalAttrList() {
        //企业名称，企业状态，最近跟进时间，创建时间，创建人，负责人
        return Arrays.asList(COMPANY_NAME.getAttr(), CLUE_STATUS.getAttr(),LAST_CONNECT_TIME.getAttr(),ADD_TIME.getAttr(),CREATOR_ID.getAttr(),OWNER_ID.getAttr());
    }

    /**
     * 市场活动详情页tab 关联销售线索列表，筛选条件的字段
     * @return
     */
    public static String[] getMarketActivityTalScreenList() {
        //企业名称，企业状态，最近跟进时间，创建时间，创建人，负责人
        return new String[]{ADD_TIME.getAttr(), CLUE_STATUS.getAttr()};
    }

    public static String getI18nValue(String value) {
        return I18nMessageUtil.getMessage(value);
    }

    public static ClueEnum getByAttr(String attr) {
        for (ClueEnum clueEnum : values()) {
            if (Objects.equals(clueEnum.getAttr(), attr)) {
                return clueEnum;
            }
        }
        return null;
    }
}
